/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Authenticate;

import AccessPermission.UserAccessPermissionBean;
import Module.ModuleBean;
import SubModule.SubModuleBean;
import User.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

/**
 *
 * @author AbhaySahu
 */
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        try {
            String user = request.getParameter("user");
            String pass = request.getParameter("pw");
            if (request.getParameter("admin").equals("0")) {
                int i = Authenticate.Query.AuthenticateLogin(user, pass);
                if (i != 0) {
                    if (AccessPermission.Query.getUserStatus(user)) {
                        session = request.getSession();
                        session.setAttribute("uid", i);
                        Vector<Object> list = AccessPermission.Query.userAccessPermissionViewAll(i);
                        Iterator it = list.iterator();
                        while (it.hasNext()) {
                            UserAccessPermissionBean ub = (UserAccessPermissionBean) it.next();
                            SubModuleBean sb = ub.getSubModuleBean();
                            ModuleBean mb = ub.getModuleBean();
                            session.setAttribute(sb.getSubModuleName(), ub.getAcessStatus());
                        }
                        Vector<Object> list1 = Module.Query.viewAll();
                        Iterator it1 = list1.iterator();
                        while (it1.hasNext()) {
                            ModuleBean mb = (ModuleBean) it1.next();
                            boolean temp = AccessPermission.Query.getModuleStatus(mb.getModuleId(), i);
                            if (temp) {
                                session.setAttribute(mb.getModuleName(), "1");
                            } else {
                                session.setAttribute(mb.getModuleName(), "0");
                            }
                        }
                        response.sendRedirect("/RIT/Dashboard/Dashboard.jsp");
                    } else {
                        UserBean ub = Authenticate.Query.AuthenticateStuLogin(user);
                        if (ub.getUserType().equals("Student")) {
                            session.setAttribute("uid", i);
                            session.setAttribute("email", ub.getEmail());
                            session.setAttribute("name", ub.getEmployeeName());
                            session.setAttribute("utype", ub.getUserType());
                            response.sendRedirect("/RIT/Student/Services.jsp");
                        } else {
                            request.setAttribute("msg", "Please Contact to System Administrator!");
                            request.setAttribute("value", "0");
                            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                        }
                    }
                } else {
                    request.setAttribute("msg", "Username Password is incorrect");
                    request.setAttribute("value", "0");
                    getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                }
            } else {
                int i = Authenticate.Query.add(user, pass);
                if (i != 0) {
                    request.setAttribute("msg", "Registration Sucessfull!");
                    request.setAttribute("value", "1");
                    getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                } else {
                    request.setAttribute("msg", "Registration Failed !");
                    request.setAttribute("value", "0");
                    getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                }
            }
        } catch (Exception e) {
            request.setAttribute("msg", "Internet Connection Problem! Please Try Again Later");
            request.setAttribute("value", "0");
            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
